trigger CountContacts on Contact (after insert,after delete,after update) {
Set<Id> AcId = New Set<Id>();
Set<Id> AcId1 = New Set<Id>();
List<Id> OldAcId = New List<Id>();
Map<id,List<Contact>> idmap = New Map<Id,List<Contact>>();
Integer listsize;
 List<Contact> ConList = New List<Contact>();
  List<Contact> ConListNew = New List<Contact>();
 list<Contact> Slist = New list<Contact>();
 
 if (Trigger.isdelete) {
     for(Contact con : Trigger.old){      
        OldAcId.add(Trigger.oldMap.get(con.id).AccountId);
        System.debug('Check the old value' + OldAcId);
        }
    if(OldAcId!= NULL){
     ConList = [Select Id,AccountId from Contact  where AccountID =: OldAcId];
      }
   
   for(Contact con : Trigger.old){
    AcId.add(con.AccountId);
    List<Contact> mapconlist = New List<Contact>();  
     for(Contact c : ConList){
      if(Con.AccountId == c.AccountId){
        mapconlist.add(c);
        listsize = mapconlist.Size();
       }
       idmap.put(Con.AccountId,mapconlist);
      }      
    }    
   List<Account> acl = [Select Id from Account where Id =: idmap.keyset()];
    for(Account co : acl){
     Slist = idmap.get(co.Id);
     System.debug('Size of Map Slist values jnjnjnjn' +Slist);
     listsize = Slist.Size();
     System.debug('Size of Map values' +listsize);     
     co.Number_Of_Contacts__c = listsize;
     update co;
    }
  }
  else if (Trigger.isupdate) {
     for(Contact con : Trigger.old){      
        OldAcId.add(Trigger.oldMap.get(con.id).AccountId);
        System.debug('Check the old value' + OldAcId);
        }
    if(OldAcId!= NULL){
     ConList = [Select Id,AccountId from Contact  where AccountID =: OldAcId];
      }
   
   for(Contact con : Trigger.old){
    AcId.add(con.AccountId);
    List<Contact> mapconlist = New List<Contact>();  
     for(Contact c : ConList){
      if(Con.AccountId == c.AccountId){
        mapconlist.add(c);
        listsize = mapconlist.Size();
       }
       idmap.put(Con.AccountId,mapconlist);
      }      
    }    
   List<Account> acl = [Select Id from Account where Id =: idmap.keyset()];
    for(Account co : acl){
     Slist = idmap.get(co.Id);
     System.debug('Size of Map Slist values jnjnjnjn' +Slist);
     listsize = Slist.Size();
     System.debug('Size of Map values' +listsize);     
     co.Number_Of_Contacts__c = listsize;
     update co;
    }
  for(Contact con : Trigger.New){
     AcId1.add(con.AccountId);
    }
  System.debug('Sample New Account Id' + AcId1);
  if(AcId1!= NULL){
     ConListNew = [Select Id,AccountId from Contact  where AccountID =: AcId1];
      }  
   for(Contact con : Trigger.New){
    AcId.add(con.AccountId);
    List<Contact> mapconlist = New List<Contact>();  
     for(Contact c : ConListNew){
      if(Con.AccountId == c.AccountId){
        mapconlist.add(c);
        listsize = mapconlist.Size();
       }
       idmap.put(Con.AccountId,mapconlist);
      }      
    }
   List<Account> acl1 = [Select Id from Account where Id =: idmap.keyset()];
    for(Account co : acl1){
     Slist = idmap.get(co.Id);
     System.debug('Size of Map Slist values jnjnjnjn' +Slist);
     listsize = Slist.Size();
     System.debug('Size of Map values' +listsize);     
     co.Number_Of_Contacts__c = listsize;
     update co;
    }   
   }           
 else {
  for(Contact con : Trigger.New){
      AcId.add(con.AccountId);  
      }
 if(AcId != NULL){
    ConList = [Select Id,AccountId from Contact  where AccountID =: AcId];
    }
   System.debug('Check List' + ConList);
   for(Contact con : Trigger.New){
    AcId.add(con.AccountId);
    List<Contact> mapconlist = New List<Contact>();  
     for(Contact c : ConList){
      if(Con.AccountId == c.AccountId){
        mapconlist.add(c);
        listsize = mapconlist.Size();
       }
       idmap.put(Con.AccountId,mapconlist);
      }      
    }
   List<Account> acl = [Select Id from Account where Id =: idmap.keyset()];
    for(Account co : acl){
     Slist = idmap.get(co.Id);
     System.debug('Size of Map Slist values jnjnjnjn' +Slist);
     listsize = Slist.Size();
     System.debug('Size of Map values' +listsize);     
     co.Number_Of_Contacts__c = listsize;
     update co;
    }
  }
   System.debug('Size of Map' + idmap.Size());
   System.debug('Check the map of acId and con list' + idmap);
 
   // Ac.Number_Of_Contacts__c = Cont;        
  }